#ifndef ALERTING_H
#define ALERTING_H

#include <string>
#include <vector>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <chrono>
#include <atomic>
#include <condition_variable>
#include <queue>

// 告警级别
enum class AlertLevel {
    INFO,
    WARNING,
    ERROR,
    CRITICAL
};

// 告警对象
struct Alert {
    std::string name;
    std::string description;
    AlertLevel level;
    std::string source;
    std::unordered_map<std::string, std::string> labels;
    std::chrono::time_point<std::chrono::system_clock> timestamp;
    
    Alert(const std::string& name, const std::string& description, AlertLevel level, 
          const std::string& source, 
          const std::unordered_map<std::string, std::string>& labels = {});
};

// 告警通知渠道接口
class AlertNotifier {
public:
    virtual ~AlertNotifier() = default;
    
    // 发送告警通知
    virtual bool notify(const Alert& alert) = 0;
    
    // 获取通知渠道名称
    virtual std::string getName() const = 0;
};

// 邮件通知实现
class EmailNotifier : public AlertNotifier {
public:
    EmailNotifier(const std::string& smtpServer, int smtpPort, 
                const std::string& username, const std::string& password,
                const std::vector<std::string>& recipients);
    
    bool notify(const Alert& alert) override;
    std::string getName() const override;
    
private:
    std::string smtpServer_;
    int smtpPort_;
    std::string username_;
    std::string password_;
    std::vector<std::string> recipients_;
};

// Webhook通知实现
class WebhookNotifier : public AlertNotifier {
public:
    WebhookNotifier(const std::string& webhookUrl);
    
    bool notify(const Alert& alert) override;
    std::string getName() const override;
    
private:
    std::string webhookUrl_;
};

// 告警规则接口
class AlertRule {
public:
    virtual ~AlertRule() = default;
    
    // 检查是否需要触发告警
    virtual bool check() = 0;
    
    // 获取当告警被触发时的告警对象
    virtual Alert getAlert() = 0;
};

// 阈值告警规则
class ThresholdAlertRule : public AlertRule {
public:
    ThresholdAlertRule(const std::string& name, const std::string& description, 
                     AlertLevel level, const std::string& source,
                     std::function<double()> valueProvider,
                     double threshold, 
                     std::function<bool(double, double)> comparator);
    
    bool check() override;
    Alert getAlert() override;
    
private:
    std::string name_;
    std::string description_;
    AlertLevel level_;
    std::string source_;
    std::function<double()> valueProvider_;
    double threshold_;
    std::function<bool(double, double)> comparator_;
};

// 告警管理器
class AlertManager {
public:
    static AlertManager& instance();
    
    // 添加告警通知渠道
    void addNotifier(std::shared_ptr<AlertNotifier> notifier);
    
    // 添加告警规则
    void addRule(std::shared_ptr<AlertRule> rule);
    
    // 发送告警
    void sendAlert(const Alert& alert);
    
    // 开始告警检查
    void start(std::chrono::seconds checkInterval = std::chrono::seconds(60));
    
    // 停止告警检查
    void stop();
    
private:
    AlertManager() = default;
    ~AlertManager();
    
    // 检查线程函数
    void checkLoop();
    
    std::vector<std::shared_ptr<AlertNotifier>> notifiers_;
    std::vector<std::shared_ptr<AlertRule>> rules_;
    std::mutex notifiersMutex_;
    std::mutex rulesMutex_;
    
    // 告警队列和处理线程
    std::queue<Alert> alertQueue_;
    std::mutex queueMutex_;
    std::condition_variable queueCondition_;
    std::thread processingThread_;
    std::atomic<bool> running_;
    std::chrono::seconds checkInterval_;
    
    // 告警处理线程函数
    void processAlerts();
};

// 快捷方法创建告警规则
std::shared_ptr<AlertRule> createThresholdAlertRule(
    const std::string& name, const std::string& description, 
    AlertLevel level, const std::string& source,
    std::function<double()> valueProvider,
    double threshold,
    const std::string& comparison);

#endif // ALERTING_H 